package com.sx.pipeline.ui.service

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.bus.RxBus
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.event.DeviceEvent
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.EquipmentAddDTO
import com.sx.pipeline.repos.http.req.ServiceAddDTO
import com.sx.pipeline.repos.http.res.DeviceInfoVo
import com.sx.pipeline.repos.http.res.Ipage
import com.sx.pipeline.repos.http.res.ReleaseTaskVo
import com.sx.pipeline.repos.http.res.ServiceTitleVO

/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description: TODO
 */
class ServiceVM(app: Application) : CYBaseViewModel(app) {
    // 省ID
    val provinceIDField: ObservableField<Int> = ObservableField()

    // 市ID
    val cityIDField: ObservableField<Int> = ObservableField()

    // 区ID
    val areaIDField: ObservableField<Int> = ObservableField()

    // 地址详情
    val addressField: ObservableField<String> = ObservableField()

    // 经度
    val longitudeField: ObservableField<String> = ObservableField()

    // 纬度
    val latitudeField: ObservableField<String> = ObservableField()

    /**
     * 添加设备
     */
    fun addServiceDevice(
        titleId: Int, //标题id
        title: String,
        price: String,
        describe: String, //描述
        address:String,
        contact: String, //联系人
        contactPhone: String,//联系电话
        images: String,//设备图片
    ) {


        if (TextUtils.isEmpty(describe)) {
            ToastUtils.showShort("请输入内容描述")
            return
        }
        if (TextUtils.isEmpty(price)) {
            ToastUtils.showShort("请输入服务价格")
            return
        }
        if (TextUtils.isEmpty(address)) {
            ToastUtils.showShort("请选择服务区域")
            return
        }
        if (TextUtils.isEmpty(contact)) {
            ToastUtils.showShort("请填写联系人")
            return
        }
        if (TextUtils.isEmpty(contactPhone)) {
            ToastUtils.showShort("请填写联系电话")
            return
        }
        if (TextUtils.isEmpty(images)) {
            ToastUtils.showShort("请上传设备图片")
            return
        }
        val dto = ServiceAddDTO(
            0,
            titleId, title,price, describe,
            provinceIDField.get()!!,
            cityIDField.get()!!, areaIDField.get()!!,address, contact, contactPhone, images
        )
        addSubscribe(
            mModel.addServiceDevice(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        ToastUtils.showShort("设备添加完成")
                        //通知列表刷新
                        RxBus.getDefault().post(DeviceEvent())
                        finish()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")

                    }
                })
        )
    }

    /**
     * 编辑设备
     */
    fun editServiceDevice(
        deviceId:Int,
        titleId: Int, //标题id
        title: String,
        price:String,
        describe: String, //描述
        address:String,
        contact: String, //联系人
        contactPhone: String,//联系电话
        images: String,//设备图片
    ) {


        if (TextUtils.isEmpty(describe)) {
            ToastUtils.showShort("请输入内容描述")
            return
        }
        if (TextUtils.isEmpty(price)) {
            ToastUtils.showShort("请输入服务价格")
            return
        }
        if (TextUtils.isEmpty(address)) {
            ToastUtils.showShort("请选择服务区域")
            return
        }
        if (TextUtils.isEmpty(contact)) {
            ToastUtils.showShort("请填写联系人")
            return
        }
        if (TextUtils.isEmpty(contactPhone)) {
            ToastUtils.showShort("请填写联系电话")
            return
        }
        if (TextUtils.isEmpty(images)) {
            ToastUtils.showShort("请上传设备图片")
            return
        }
        val dto = ServiceAddDTO(
            deviceId,
            titleId, title,price, describe,
            provinceIDField.get()!!,
            cityIDField.get()!!, areaIDField.get()!!, address,contact, contactPhone, images
        )
        addSubscribe(
            mModel.editServiceDevice(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {

                    override fun onResult(result: Any) {
                        ToastUtils.showShort("设备编辑完成")
                        //通知列表刷新
                        RxBus.getDefault().post(DeviceEvent())
                        finish()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")

                    }
                })
        )
    }
    /**
     * 获取服务标题列表
     */
    val serviceTitleListEvent: SingleLiveEvent<List<ServiceTitleVO>> = SingleLiveEvent()

    //服务标题
    val serviceTitleData: MutableList<String> = ArrayList()
    fun getServiceTitle() {
        addSubscribe(
            mModel.getServiceTitle()
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<List<ServiceTitleVO>, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: List<ServiceTitleVO>) {
                        for (data in result) {
                            serviceTitleData?.add(data.title)
                        }
                        serviceTitleListEvent.postValue(result)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 获取台账设备列表
     */
    var page = 1
    val deviceListEvent: SingleLiveEvent<List<ServiceAddDTO>> = SingleLiveEvent()
    fun getPmsServiceList(key: String,limit:Int) {
        addSubscribe(
            mModel.getPmsServiceList(key, page,limit)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<ServiceAddDTO>, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Ipage<ServiceAddDTO>) {
                        deviceListEvent.postValue(result.records)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 获取服务设备详情
     */
    val deviceDetailDataEvent: SingleLiveEvent<ServiceAddDTO> = SingleLiveEvent()
    fun getServiceDeviceDetail(id: Int) {
        addSubscribe(
            mModel.getServiceDeviceDetail(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<ServiceAddDTO, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: ServiceAddDTO) {
                        deviceDetailDataEvent.postValue(result)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 删除服务台账设备
     */
    fun getServiceDelete(id: Int) {
        addSubscribe(
            mModel.getServiceDelete(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        ToastUtils.showShort("设备删除完成")
                        //通知列表刷新
                        RxBus.getDefault().post(DeviceEvent())
                        finish()
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }
}